import loadData from '../services/homeService';
import { routerRedux } from 'dva/router';
export default {

  namespace: 'home',

  state: {
    contentData:{},
    showContent:false,
    showLoading:false,
    showEmpty:false,
    showError:false,
    showFail:false,
    showRefreshing:false,
    emptyMsg:'',
    errorMsg:'',
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      dispatch({
        type:'firstLoadData'
      })
    },
  },

  effects: {
    *firstLoadData({ payload }, { call, put }) {  // eslint-disable-line
      console.log('first load data');
      yield put({
        type: 'onFirstLoadData'
      });
      const result = yield call(loadData, {});
      yield put({
        type: 'onLoadSuccess',
        payload: { ...result }
      });
    },

    *click({payload}, { call, put }) {
      console.log('click');
      yield put(routerRedux.push({
        pathname:'/itemlist',
        query:{
          payload: payload
        }
      }));
    }
  },

  reducers: {

    onFirstLoadData(state, action) {
      return {
        ...state,
        showLoading:true,
        showContent:false,
        showEmpty:false,
        showError:false,
        showFail:false,
        showRefreshing:false,
      };
    },

    onLoadSuccess(state, { payload }) {
      return {
        ...state,
        contentData: payload,
        showContent: true,
        showLoading: false,
        showEmpty: false,
        showError: false,
        showFail: false,
        showRefreshing: false,
      };
    }
  },

};
